import {useRef, useCallback, useMemo} from 'react'
import {message} from 'antd'
import {StaffService} from '@/services/staff';
import {ActionType} from '@ant-design/pro-components';
import { useModel } from '@umijs/max';

// 这里开发的心智负担很高，这里要负责承上启下，要提前预见到整个Staff完整功能
// 分析出哪些东西需提练到当前model中，便于后续所有组件使用
export default () => {

  
  // const {staffCategory}=useModel('dic')
  // const staffCategoryEnum=useMemo(()=>{
  //   const result=new Map<number, {text: string}>()
  //   result.set(0,{text:'--请选择--'})
  //   staffCategory.forEach(item=>{
  //     result.set(item.dic_id,{text:item.dic_name})
     
  //   })
  //    return result;
  // },[staffCategory])

  const {staffCategory}=useModel('dic')

   const staffCategoryEnum = useMemo(() => {
     const result = new Map();
     result.set(0, { text: '- please choose -' });
     staffCategory.forEach((item) =>
       result.set(item.dic_id, { text: item.dic_name }),
     );
     return result;
   }, [staffCategory]);
  
  // 表格引用
  const actionRef = useRef<ActionType>();

  // 分页查询员工数据
  const handleQuery = useCallback(async (body: API.Staff_Query_Body) => {
    return StaffService.queryStaffList(body)
  }, [])

  // 处理离职
  const handleDismiss = useCallback(async (staff: API.StaffInfo) => {
    const hide = message.loading('dismissing..')
    try {
      await StaffService.dismissStaff(staff.stf_id)
      hide()
      message.success(`Staff："${staff.stf_name}" dismiss success！`)
      actionRef.current?.reloadAndRest?.()
      return true
    } catch(error) { 
      hide()
      message.error(`Staff："${staff.stf_name}" dismiss fail，please retry！`)
      return false
    }
  }, [])

  // 处理复职
  const handleReinstate = useCallback(async (staff: API.StaffInfo) => {
    const hide = message.loading('reinstating..')
    try {
      await StaffService.reinstateStaff(staff.stf_id)
      hide()
      message.success(`Staff："${staff.stf_name}" reinstate success！`)
      actionRef.current?.reloadAndRest?.()
      return true
    } catch(error) {
      hide()
      message.error(`Staff："${staff.stf_name}" reinstate fail，please retry！`)
      return false
    }
  }, [])

  // 处理新增
  const handleAdd = useCallback(async (staff: API.Staff_Add_Body) => {
    const hide = message.loading(`Staff: "${staff.stf_name}" is adding..`);
    try {
      await StaffService.addStaff({ ...staff });
      hide();
      message.success(`Staff: "${staff.stf_name}" add success！`);
      actionRef.current?.reload?.()
      return true;
    } catch (error) {
      hide();
      message.error(`Staff: "${staff.stf_name}" add fail，please retry！`);
      return false;
    }
  }, [])

  // 处理修改
  const handleUpdate = useCallback(async (staff: API.Staff_Modify_Body) => {
    const hide = message.loading('Staff updating..');
    try {
      await StaffService.modifyStaff({...staff})
      hide()
      message.success(`Staff update success！`);
      actionRef.current?.reload?.()
      return true    
    } catch (error) {
      hide();
      message.error('State update fail, please retry！');
      return false;
    }
  }, [])

  return {
    staffCategory,
    staffCategoryEnum,
    actionRef,
    handleQuery,
    handleDismiss,
    handleReinstate,
    handleAdd,
    handleUpdate,
  };
}

